package com.cola.action;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.cola.common.ConstCommon;
import com.cola.entity.module.ParentModule;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
/**
 * Created by Max on 2017/8/17.
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware {

    private static final Logger logger = LoggerFactory.getLogger(BaseAction.class);
    private HttpServletRequest request;
    private HttpServletResponse response;


    /**
     * 获取当前的request对象
     *
     * @return
     */
    public HttpServletRequest getRequest() {
        return request;
    }

    /**
     * 获取当前的response对象
     *
     * @return
     */
    public HttpServletResponse getResponse() {
        return response;
    }

    //设置request对象
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    //设置response对象
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    /**
     * 设置request参数
     *
     * @param name  参数名
     * @param value 参数值
     */
    public void setRequestAttribute(String name, Object value) {
        request.setAttribute(name, value);
    }

    /**
     * 设置SessionAttribute
     *
     * @param name  参数名
     * @param value 参数值
     */
    public void setSessionAttribute(String name, Object value) {
        request.getSession().setAttribute(name, value);
    }

    /**
     * 获取SessionAttribute
     *
     */
    public Object getSessionAttribute(String key) {
        return request.getSession().getAttribute(key);
    }


    /**
     * 设置错误信息
     * @param errorInfo
     */
    public void setErrorInfoToRequest(String errorInfo){
        setRequestAttribute("errorInfo",errorInfo);
    }

    /**
     * 设置菜单
     */
    public void setModule(){
        String menuJson = getSessionAttribute(ConstCommon.USERMODULE).toString();
        List<ParentModule> moduleList = JSONObject.parseObject(menuJson,new TypeReference<List<ParentModule>>(){});
        setRequestAttribute(ConstCommon.MODULES,moduleList);
    }

    public void setUserInfo(){
        setRequestAttribute(ConstCommon.USERCONTEXT,getSessionAttribute(ConstCommon.USERCONTEXT));
    }

    public Long getUserId(){
        Map<String,Object> userMap = (Map<String,Object>)getSessionAttribute(ConstCommon.USERCONTEXT);
        return (Long)userMap.get("system_user_id");
    }

    public String getUserName(){
        Map<String,Object> userMap = (Map<String,Object>)getSessionAttribute(ConstCommon.USERCONTEXT);
        return userMap.get("system_user_name").toString();
    }

    public Integer getUserRoleId(){
        Map<String,Object> userMap = (Map<String,Object>)getSessionAttribute(ConstCommon.USERCONTEXT);
        return (Integer)userMap.get("role_id");
    }

}